import org.junit.Test;

import java.sql.*;

public class JdbcLearn {
//    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        JdbcLearn jdbcLearn = new JdbcLearn();
//     //   jdbcLearn.selectData();
//        jdbcLearn.insert();
//        jdbcLearn.selectData();
//
//    }

    @Test
    public void insert() throws SQLException, ClassNotFoundException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取链接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/lulaoshi?useSSL=false&serverTimezone=UTC",
                "root",
                "12345678");
        //3.定义sql

        String sql="insert into t_user values(null,'李同学999','5555',18,'女','8899@qq.com')";

        //4.创建数据操作对象
        Statement statement = connection.createStatement();

        //5.执行sql，获取结果集
        int i = statement.executeUpdate(sql);
        System.out.println("向数据库中插入了i = " + i);

        //6. 关闭资源
        //关闭资源
        statement.close();
        connection.close();
    }
    /**
     * 数据的查询
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    @Test
    public void selectData() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取链接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/lulaoshi?useSSL=false&serverTimezone=UTC",
                "root",
                "12345678");
//        System.out.println("connection = " + connection);

        //3.实现Crud
        String sql = "select * from user";
        //4.创建数据操作对象
        Statement statement = connection.createStatement();
        //5.执行sql，获取结果集
        ResultSet resultSet = statement.executeQuery(sql);

        //6.通过next指向结果集第一行
        while (resultSet.next()) {
            //7.解析结果中数据字段
            System.out.println("resultSet.getInt(\"id\") = " + resultSet.getInt("id"));
            System.out.println("resultSet.getInt(\"username\") = " + resultSet.getString("username"));
            System.out.println("resultSet.getString(\"password\") = " + resultSet.getString("password"));
            System.out.println("--------------------------------");
        }
        //关闭资源
        statement.close();
        connection.close();

    }

    /**
     * 删除某条记录
     * @throws SQLException
     * @throws ClassNotFoundException
     */

    @Test
    public void deleteDb() throws SQLException, ClassNotFoundException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取链接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/lulaoshi?useSSL=false&serverTimezone=UTC",
                "root",
                "12345678");
        //3.定义sql

        String name="李同学";
//拼接
        String sql="delete from t_user where username='"+name+"'";

        System.out.println("sql = " + sql);


        //4.创建数据操作对象
        Statement statement = connection.createStatement();

        //5.执行sql，获取结果集
        int i = statement.executeUpdate(sql);
        System.out.println("删除数据库中 " + i);

        //6. 关闭资源
        //关闭资源
        statement.close();
        connection.close();
    }
}